package com.sgcc.sx.pmos.mobileapp.dialog;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.common.bean.RatioCurveBean;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvcNetCallback;
import com.sgcc.sx.pmos.common.view.LineChartView;
import com.sgcc.sx.pmos.e_sx_mall.adapter.ChartSelectAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * 新增户号-----地方电网弹窗
 */
public class ChartLinSelectPop extends PartShadowPopupView {
    ChartSelectAdapter adapter;
    Context context;
    RecyclerView recyclerView;
    List<RatioCurveBean.Bean> list = new ArrayList<>();
    TextView tv_cancel, tv_positive,txt_chart_name;
    int chahrtposition = 0;
    LinearLayout  llcontent ;
    int position;

    public ChartLinSelectPop(@NonNull Context context, OnClickListener onClickListener) {
        super(context);
        this.context = context;
        this.onClickListener = onClickListener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.chart_lin_select_pop;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tv_positive = findViewById(R.id.tv_positive);
        tv_positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickListener != null) {
                    onClickListener.onPositiveClick(list.get(chahrtposition));
                }
                dismiss();
            }
        });
        txt_chart_name= findViewById(R.id.txt_chart_name);
        recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManagerChart = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManagerChart);
        llcontent = findViewById(R.id.ll_content);
        adapter = new ChartSelectAdapter(context);

        adapter.setItemClickListener(new ChartSelectAdapter.OnItemClickListener() {
            @Override
            public void OnItemsClickListener(int position, RatioCurveBean.Bean s, int lastPosotion) {
                txt_chart_name.setVisibility(VISIBLE);
                txt_chart_name.setText(s.getRatioName());
                llcontent.removeAllViews();
                LineChartView lineChartView = new LineChartView(context, s.getYAxis());
                llcontent.addView(lineChartView);
                list.get(lastPosotion).setSelect(false);
                list.get(position).setSelect(true);
                adapter.notifyDataSetChanged();
                chahrtposition = position;
            }
        });
        getChartList();
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

//    @Override
//    protected int getMaxHeight() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()));
//    }

    @Override
    protected int getMaxWidth() {
        return (int) (XPopupUtils.getScreenWidth(getContext()));
    }

    @Override
    protected boolean onBackPressed() {
        return true;
    }


    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickListener onClickListener;


    public interface OnClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(RatioCurveBean.Bean bean);

    }

    private ArrayList<RatioCurveBean.Bean> ratioCurveList;

    public void getChartList() {

        HttpRequestUtils.getInstance().request(new HashMap<>(), HttpInterConst.GET_RATIO_CURVE, new MvcNetCallback<RatioCurveBean>() {
            @Override
            public void onResponse(RatioCurveBean response, int id) {
                super.onResponse(response, id);
                if (null != response) {
                    ratioCurveList = response.getData();
                    if (null != ratioCurveList && ratioCurveList.size() > 0) {
                        list = ratioCurveList;
                        list.get(position).setSelect(true);
                        txt_chart_name.setVisibility(VISIBLE);
                        txt_chart_name.setText(list.get(position).getRatioName());
                        llcontent.removeAllViews();
                        LineChartView lineChartView = new LineChartView(context, list.get(position).getYAxis());
                        llcontent.addView(lineChartView);
                        adapter.setNewData(list);
                        adapter.notifyDataSetChanged();
                        recyclerView.setAdapter(adapter);
                    } else {
                        ToastUtils.showShort(response.getMsg());
                    }
                } else {
                    ToastUtils.showShort("曲线数据获取失败");
                }
            }
        });
    }
}
